import pandas as pd

df = pd.read_excel("data/直播时长.xlsx")
to_df = pd.DataFrame()

def to_time_func(row):
    time_str = row.replace('小时', ':').replace('分钟', ':').replace('秒', '')
    parts = time_str.split(':')
    if len(parts) == 3:
        try:
            hours, minutes, seconds = map(int, parts)
            return pd.to_timedelta(f"{hours:02}:{minutes:02}:{seconds:02}")
        except ValueError:
            return pd.NaT
    elif len(parts) == 2:
        hours = 0
        minutes, seconds = map(int, parts)
        return pd.to_timedelta(f"{hours:02}:{minutes:02}:{seconds:02}")
    else:
        return pd.NaT


df["换算时长"] = df["直播时长"].apply(to_time_func)

# 计算总时长
total_duration = df["换算时长"].sum()

# 格式化输出总时长
total_seconds = total_duration.total_seconds()
hours = int(total_seconds // 3600)
minutes = int((total_seconds % 3600) // 60)
seconds = int(total_seconds % 60)

formatted_total_duration = f"{hours:02}:{minutes:02}:{seconds:02}"

print("总直播时长:", formatted_total_duration)
print("总直播时长(秒):", total_seconds)